from .celery import app
from home.models import Banner
from django.conf import settings
from home.serializers import BannerSerizlizer
from django.core.cache import cache


@app.task
def update_banner():
    # 从数据库取出轮播图数据
    queryset = Banner.objects.all().filter(is_delete=False, is_show=True).order_by('orders')[:settings.BANNER_COUNT]
    # 序列化
    ser = BannerSerizlizer(instance=queryset, many=True)
    for item in ser.data:
        item['image'] = settings.HOST_URL + item['image']
    # 放到redis中
    cache.set('banner_list_cache', ser.data)
    return True
